/*
 * Copyright (C) 2024 pedroSG94.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pedro.streamer.utils

import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.media.MediaPlayer
import android.os.Build
import android.view.MenuItem
import android.view.Surface
import androidx.annotation.RequiresApi
import com.pedro.encoder.input.gl.SpriteGestureController
import com.pedro.encoder.input.gl.render.filters.AnalogTVFilterRender
import com.pedro.encoder.input.gl.render.filters.BasicDeformationFilterRender
import com.pedro.encoder.input.gl.render.filters.BeautyFilterRender
import com.pedro.encoder.input.gl.render.filters.BlurAreaFilterRender
import com.pedro.encoder.input.gl.render.filters.BlurFilterRender
import com.pedro.encoder.input.gl.render.filters.CircleFilterRender
import com.pedro.encoder.input.gl.render.filters.CropFilterRender
import com.pedro.encoder.input.gl.render.filters.MagnifierFilter
import com.pedro.encoder.input.gl.render.filters.NoiseFilterRender
import com.pedro.encoder.input.gl.render.filters.RippleFilterRender
import com.pedro.encoder.input.gl.render.filters.RotationFilterRender
import com.pedro.encoder.input.gl.render.filters.SnowFilterRender
import com.pedro.encoder.input.gl.render.filters.SwirlFilterRender
import com.pedro.encoder.input.gl.render.filters.TornadoFilter
import com.pedro.encoder.input.gl.render.filters.WaveFilter
import com.pedro.encoder.input.gl.render.filters.ZoomFilter
import com.pedro.encoder.input.gl.render.filters.`object`.GifObjectFilterRender
import com.pedro.encoder.input.gl.render.filters.`object`.ImageObjectFilterRender
import com.pedro.encoder.input.gl.render.filters.`object`.SurfaceFilterRender
import com.pedro.encoder.input.gl.render.filters.`object`.TextObjectFilterRender
import com.pedro.encoder.utils.gl.TranslateTo
import com.pedro.library.view.GlInterface
import com.pedro.streamer.R
import java.io.IOException

/**
 * Created by pedro on 27/2/24.
 */
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class FilterMenu(private val context: Context) {

  val spriteGestureController = SpriteGestureController()

  fun onOptionsItemSelected(item: MenuItem, glInterface: GlInterface): Boolean {
    //Stop listener for image, text and gif stream objects.
    spriteGestureController.stopListener()
    when (item.itemId) {
      R.id.no_filter -> {
        glInterface.clearFilters()
        return true
      }
      R.id.analog_tv -> {
        glInterface.setFilter(AnalogTVFilterRender())
        return true
      }
      R.id.basic_deformation -> {
        glInterface.setFilter(BasicDeformationFilterRender())
        return true
      }
      R.id.beauty -> {
        glInterface.setFilter(BeautyFilterRender())
        return true
      }
      R.id.blur -> {
        glInterface.setFilter(BlurFilterRender())
        return true
      }
      R.id.blur_area -> {
        val blurAreaFilterRender=BlurAreaFilterRender()
        glInterface.setFilter(blurAreaFilterRender)
        blurAreaFilterRender.setTouchXY(128f,128f)
        blurAreaFilterRender.setBlurRadiusArea(50)
        blurAreaFilterRender.setBlurRadius(30)
        return true
      }
      R.id.circle -> {
        glInterface.setFilter(CircleFilterRender())
        return true
      }
      R.id.crop -> {
        glInterface.setFilter(CropFilterRender().apply {
          //crop center of the image with 40% of width and 40% of height
          setCropArea(30f, 30f, 40f, 40f)
        })
        return true
      }
      R.id.gif -> {
        try {
          val gifObjectFilterRender = GifObjectFilterRender()
          gifObjectFilterRender.setGif(context.resources.openRawResource(R.raw.banana))
          glInterface.setFilter(gifObjectFilterRender)
          gifObjectFilterRender.setScale(50f, 50f)
          gifObjectFilterRender.setPosition(TranslateTo.BOTTOM)
          spriteGestureController.setBaseObjectFilterRender(gifObjectFilterRender) //Optional
        } catch (ignored: IOException) { }
        return true
      }
      R.id.image -> {
        val imageObjectFilterRender = ImageObjectFilterRender()
        glInterface.setFilter(imageObjectFilterRender)
        imageObjectFilterRender.setImage(
          BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)
        )
        imageObjectFilterRender.setScale(50f, 50f)
        imageObjectFilterRender.setPosition(TranslateTo.RIGHT)
        spriteGestureController.setBaseObjectFilterRender(imageObjectFilterRender) //Optional
        spriteGestureController.setPreventMoveOutside(false) //Optional
        return true
      }
      R.id.magnifier -> {
        val magnifierFilter=MagnifierFilter()
        glInterface.setFilter(magnifierFilter)
        magnifierFilter.radius=200f
        magnifierFilter.magnification=0.5f
        magnifierFilter.setTouchXY(540,860)
        magnifierFilter.setRadius(200)
        magnifierFilter.setMagnification(60)
        return true
      }
      R.id.noise -> {
        glInterface.setFilter(NoiseFilterRender())
        return true
      }
      R.id.ripple -> {
        val rippleFilterRender=RippleFilterRender()
        glInterface.setFilter(rippleFilterRender)
        rippleFilterRender.speed=3f
        return true
      }
      R.id.rotation -> {
        val rotationFilterRender = RotationFilterRender()
        glInterface.setFilter(rotationFilterRender)
        rotationFilterRender.rotation = 90
        return true
      }
      R.id.snow -> {
        glInterface.setFilter(SnowFilterRender())
        return true
      }
      R.id.swirl -> {
        glInterface.setFilter(SwirlFilterRender())
        return true
      }
      R.id.surface_filter -> {
        val surfaceFilterRender = SurfaceFilterRender { surfaceTexture -> //You can render this filter with other api that draw in a surface. for example you can use VLC
          val mediaPlayer: MediaPlayer = MediaPlayer.create(context, R.raw.big_bunny_240p)
          mediaPlayer.setSurface(Surface(surfaceTexture))
          mediaPlayer.start()
        }
        glInterface.setFilter(surfaceFilterRender)
        //Video is 360x240 so select a percent to keep aspect ratio (50% x 33.3% screen)
        surfaceFilterRender.setScale(48f, 27f)
        spriteGestureController.setBaseObjectFilterRender(surfaceFilterRender) //Optional
        return true
      }
      R.id.text -> {
        val textObjectFilterRender = TextObjectFilterRender()
        glInterface.setFilter(textObjectFilterRender)
        textObjectFilterRender.setText("Hello world", 22f, Color.RED)
        textObjectFilterRender.setPosition(TranslateTo.TOP_RIGHT)
        spriteGestureController.setBaseObjectFilterRender(textObjectFilterRender) //Optional
        return true
      }
      R.id.tornado -> {
        val tornadoFilter=TornadoFilter()
        glInterface.addFilter(tornadoFilter)
        tornadoFilter.setTornadoRadius(20.0f)
        tornadoFilter.setTornadoStrength(30)
        return true
      }
      R.id.wave -> {
        val waveFilter=WaveFilter();
        glInterface.addFilter(waveFilter)
        return true
      }
      R.id.zoom -> {
        val zoomFilter=ZoomFilter()
        glInterface.setFilter(zoomFilter)
        zoomFilter.setmMaxAmplitude(90f)//可大于100，
        zoomFilter.setmMaxDisplacement(30f)
        zoomFilter.setmMaxVerticalDisplacement(10f)
        zoomFilter.setmFrequency(20f)//改值权重最大
        return true
      }
      else -> return false
    }
  }
}